using System.Collections.Generic;
using Infrastructure;
using Infrastructure.DataManagement;
using Infrastructure.Hibernate;
using NHibernate;

namespace Database.Repository
{
    public abstract class BaseRepository<T> : IRepository<T> where T : class, IEntity
    {
        public readonly ISessionProvider SessionProvider;

        protected BaseRepository(ISessionProvider sessionProvider)
        {
            this.SessionProvider = sessionProvider;
        }

        protected ISession Session
        {
            get { return SessionProvider.CurrentSession; }
        }

        public void Save(T entity)
        {
            Session.SaveOrUpdate(entity);
        }

        public void Delete(T entity)
        {
            Session.Delete(entity);
        }

        public T GetById(int id)
        {
            return Session.Get<T>(id);
        }

        public IEnumerable<T> GetAll()
        {
            return Session.CreateCriteria(typeof(T)).List<T>();
        }
    }
}
